package src.thread;

import javax.management.RuntimeMBeanException;
import java.beans.Beans;

public class SyncDemo {
        public static void main(String[] args) {
                Table table=new Table();
                Thread t1=new Thread("jun"){
                        public void run(){
                                while (true){
                                        int bean=table.getBeans();
                                        Thread.yield();
                                        System.out.println(getName()+":"+bean);
                                }
                        }
                };
                Thread t2=new Thread("xin"){
                        public void run(){
                                while (true){
                                        int bean=table.getBeans();
                                        Thread.yield();
                                        System.out.println(getName()+":"+bean);
                                }
                        }
                };
                t1.start();
                t2.start();
        }
}
class  Table{
        private int Beans=20;
        public synchronized  int getBeans(){
                if (Beans==0){
                        throw new RuntimeException("没有豆子了！");
                }
                Thread.yield();
             return Beans--;   
        }
}
